package slave.sspGUI;

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ImageIcon;
import javax.swing.JComboBox;
import javax.swing.WindowConstants;
import javax.swing.JFrame;
import javax.swing.JLabel;
import slave.SlaveStreamProvider;
@SuppressWarnings("serial")
public class TopPanel extends javax.swing.JPanel {

	{
		//Set Look & Feel
		try {
			javax.swing.UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
		} catch(Exception e) {
			e.printStackTrace();
		}
	}

	private JLabel providerICon;
	private JLabel outputCoding;
	private JLabel deviceUsed;
	public JLabel getDeviceUsed() {
		return deviceUsed;
	}

	private JLabel currentCodec;
	public JLabel getCurrentCodec() {
		return currentCodec;
	}

	private JComboBox coding;
	public JComboBox getCoding() {
		return coding;
	}

	private JLabel streamcodingtext;
	private JLabel device;
	private SlaveStreamProvider ssp;
	private StreamProviderMonitor spm;
	private boolean first = true;

	/**
	 * Auto-generated main method to display this 
	 * JPanel inside a new JFrame.
	 */
	public static void main(String[] args) {
		JFrame frame = new JFrame();
		frame.getContentPane().add(new TopPanel(null, null));
		frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
		frame.pack();
		frame.setVisible(true);
	}

	public TopPanel(StreamProviderMonitor newJFrame, SlaveStreamProvider ssp) {
		super();
		this.spm=newJFrame;
		this.ssp=ssp;
		initGUI();
	}

	private void initGUI() {
		try {
			AnchorLayout thisLayout = new AnchorLayout();
			this.setLayout(thisLayout);
			setPreferredSize(new Dimension(350,150));
			{
				currentCodec = new JLabel();
				this.add(currentCodec, new AnchorConstraint(523, 1007, 623, 441, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL));
				currentCodec.setPreferredSize(new java.awt.Dimension(198, 15));
				currentCodec.setText("No active stream.");
				currentCodec.setFont(new java.awt.Font("AlArabiya",0,12));
				currentCodec.setBackground(new java.awt.Color(29,95,226));
				currentCodec.setForeground(new java.awt.Color(6,20,224));
			}
			{
				deviceUsed = new JLabel();
				this.add(deviceUsed, new AnchorConstraint(810, 798, 910, 672, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL));
				deviceUsed.setText("1");
				deviceUsed.setPreferredSize(new java.awt.Dimension(44, 15));
				deviceUsed.setFont(new java.awt.Font("AlArabiya",0,12));
				deviceUsed.setForeground(new java.awt.Color(6,20,224));
			}
			ImageIcon image = new ImageIcon("./media/provider.png");
			{		
				coding = new JComboBox();
				this.add(coding, new AnchorConstraint(650, 958, 776, 424, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL));
				coding.setPreferredSize(new java.awt.Dimension(187, 19));
				coding.setFont(new java.awt.Font("AlArabiya",0,12));
				ActionListener listener = new ActionListener() {
					@Override
					public void actionPerformed(ActionEvent e) {
						try{
							JComboBox cb = (JComboBox)e.getSource();
							int codec = (int)cb.getSelectedIndex();		
							if(first){			
								currentCodec.setText(spm.message.elementAt((ssp.currentQuality)+1));
								first = false;
							}
							else{
								currentCodec.setText((String) cb.getSelectedItem());
								ssp.fireChangeCoding(codec);	
								spm.set="NONSETTATO";
								spm.history.append("GUI: Fire change coding event!!"+"\n");
								spm.history.setCaretPosition(spm.history.getDocument().getLength());

								System.out.println("TEST CAMBIO DI CODIFICA:"+codec);	
							}

						}catch(Exception ex){
							ex.printStackTrace();	
						}
					}	

				};
				coding.addActionListener(listener );
			}
			{
				streamcodingtext = new JLabel();
				this.add(streamcodingtext, new AnchorConstraint(663, 618, 763, 64, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL));
				streamcodingtext.setText("Change stream coding");
				streamcodingtext.setPreferredSize(new java.awt.Dimension(194, 15));
				streamcodingtext.setFont(new java.awt.Font("AlArabiya",0,12));
			}
			{
				device = new JLabel();
				this.add(device, new AnchorConstraint(796, 690, 923, 64, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL));
				device.setText("Number of local bluetooth device used:");
				device.setPreferredSize(new java.awt.Dimension(219, 19));
				device.setFont(new java.awt.Font("AlArabiya",0,12));
			}
			{
				outputCoding = new JLabel();
				this.add(outputCoding, new AnchorConstraint(523, 424, 623, 64, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL));
				outputCoding.setText("Current stream coding:");
				outputCoding.setPreferredSize(new java.awt.Dimension(126, 15));
				outputCoding.setFont(new java.awt.Font("AlArabiya",0,12));
			}
			{
				providerICon = new JLabel();
				AnchorLayout providerIConLayout = new AnchorLayout();
				providerICon.setLayout(providerIConLayout);
				this.add(providerICon, new AnchorConstraint(43, 984, 483, 52, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL));
				providerICon.setText("RTPBluetooth Stream Provider ");
				providerICon.setIcon(image);
				providerICon.setPreferredSize(new java.awt.Dimension(326, 66));
				providerICon.setFont(new java.awt.Font("Lucida Sans",1,16));
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
